<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @todo 	元数据分组控制器
 * @copyright	
 * @author shiqiliang<ozil1987@gmail.com>
 * @package package_declaration
 * @subpackage mysite
 * @version 2013-12-13
 */
class Metagroups extends MY_ADMIN_Controller{
	
	function __construct()
	{
		parent::__construct();
		$this->loadModel('metadatagroup_model','Metagroups');
	}
	
	function create(){
		if($this->input->post(null,true)){
			return $this->save('metagroups/create');
		}else{
			$this->display();
		}
	}
	
	function edit($id){
		if($this->input->post(null,true)){
			return $this->save('metagroups/edit');
		}else{
			$id = $id>0?$this->input->get('id',true):intval($this->input->get('id',true));
			$res = $this->Metagroups->findByPk($id);
			if(empty($res)){
				return $this->showMessage('error_param',getRedirect(getUrl(false)));
			}
			$this->assign['metagroup'] = $res[0];
			$this->display();
		}
	}
	
	function remove($id=null){
		$redirect = getRedirect(getUrl(false));
		$id = $id>0?$id:intval($this->input->get('id',true));
		$res = $this->metagroups->findByPk($id);
		if(empty($res)){
			return $this->showMessage('error_param',$redirect);
		}
		$this->loadModel('metadatas_model','Metadatas');
		$cond = array('where'=>array('group_id'=>$res[0]['id']));
		$result = $this->Metadatas->find($cond);
		if(!empty($result)){
			return $this->showMessage('error_has_sub',$redirect);
		}
		if($this->Module->removeByPks($id) === true){
			$mes = 'mes_success';
			return $this->showMessage($mes,$redirect);
		}
		return $this->showMessage('failure',$redirect);
	}
	
	private function save($type=''){
		$redirect = getRedirect(getUrl(false));
		
		$this->load->library('form_validation');
		$this->lang->load('template');
		if ($this->form_validation->run($type) == FALSE){
			return $this->showMessage(validation_errors()); 
		}
		
		if(($id = $this->Metagroups->save($this->input->post(null,true)))!==false){
			$mes = 'mes_success';
			return $this->showMessage($mes,$redirect);
		}else{
			return $this->showMessage($this->Metagroups->_error); 
		}
	}
	
	
}